import React, { useState, useEffect } from 'react'
import { Spin, Progress } from 'antd'
import { LoadingOutlined } from '@ant-design/icons'

interface LoadingProps {
  size?: 'small' | 'default' | 'large'
  tip?: string
  fullScreen?: boolean
  showProgress?: boolean
}

const Loading: React.FC<LoadingProps> = ({
  size = 'default',
  tip = '加载中...',
  fullScreen = false,
  showProgress = false
}) => {
  const [progress, setProgress] = useState(0)

  useEffect(() => {
    if (!showProgress) return

    // 模拟加载进度（实际应该根据真实加载进度更新）
    const interval = setInterval(() => {
      setProgress(prev => {
        if (prev >= 90) {
          clearInterval(interval)
          return 90 // 停在90%，等待实际加载完成
        }
        return prev + Math.random() * 20
      })
    }, 300)

    return () => clearInterval(interval)
  }, [showProgress])

  const antIcon = <LoadingOutlined style={{ fontSize: size === 'large' ? 48 : size === 'small' ? 16 : 24 }} spin />

  if (fullScreen) {
    return (
      <div className="fixed inset-0 bg-white bg-opacity-80 flex items-center justify-center z-50">
        <div className="flex flex-col items-center gap-4">
          <Spin indicator={antIcon} size={size} tip={tip} />
          {showProgress && (
            <div className="w-64">
              <Progress
                percent={Math.floor(progress)}
                strokeColor={{
                  '0%': '#108ee9',
                  '100%': '#87d068',
                }}
                showInfo={false}
              />
            </div>
          )}
        </div>
      </div>
    )
  }

  return (
    <div className="flex flex-col items-center justify-center py-8 gap-4">
      <Spin indicator={antIcon} size={size} tip={tip} />
      {showProgress && (
        <div className="w-64">
          <Progress
            percent={Math.floor(progress)}
            strokeColor={{
              '0%': '#108ee9',
              '100%': '#87d068',
            }}
            showInfo={false}
          />
        </div>
      )}
    </div>
  )
}

export default Loading